package com.newhero.product.module.msg.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.newhero.product.common.controller.BaseController;

@Controller
@RequestMapping("/desktopMsg")
public class DesktopMsgController extends BaseController {
	
	

	@RequestMapping(value = "/login.html", method = RequestMethod.GET)
	public String login() {
		return "msg/login";
	}
	
	@RequestMapping(value = "/index.html", method = RequestMethod.GET)
	public String index(
			@RequestParam(value = "userId", defaultValue = "", required = false) String userId,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		
		List<Map<String,Object>> msgList = new ArrayList<Map<String,Object>>();
		//根据用户ID拿未读消息的逻辑
		//注意没有Session用户的，需根据userId获取所需信息
		
		String userName = "卢东宏";
		
		String sql = "SELECT U.RID,U.CONTENTID,I.DEPLOYID,U.APPID,U.USERID,U.USERNAME,U.MOBILE,"
				+ "U.TXTIME2,U.TXCOUNT2,U.YTXCOUNT2,U.CLCOUNT2,U.RETRYCOUNT2,U.TXINTERVAL2,"
				+ "I.TSCONTENT,S.USERLOGINNAME,S.NOTICETYPE,S.ORGANID "
				+ "FROM CF_NOTICEUSER U "
				+ "LEFT JOIN CF_NOTICEINFO I ON U .CONTENTID = I.RID "
				+ "LEFT JOIN SYS_USER S ON S.USERID = U.USERID "
				+ "WHERE U.ZHUOMIAN = '1' AND U.STATE2 = '0' "
				+ "AND u.USERNAME = '卢东宏' "
				+ "ORDER BY CLCOUNT2,TXTIME2";
		
		//测试数据
		Map<String,Object> msg = new HashMap<String,Object>();
		msg.put("msgId", "1");
		msg.put("msgType", "会议通知");
		msg.put("msgTitle", "关于《xxx》的会议通知，请登录系统查收。");
		msg.put("msgTime", "今天 8:25");
		msgList.add(msg);
		Map<String,Object> msg2 = new HashMap<String,Object>();
		msg2.put("msgId", "2");
		msg2.put("msgType", "明传电报");
		msg2.put("msgTitle", "您有一件明传电报待收，编号1003，请登录系统查收。");
		msg2.put("msgTime", "昨天 17:13");
		msgList.add(msg2);
		
		int msgCount = msgList.size();
		
		request.setAttribute("msgCount", msgCount);
		request.setAttribute("msgList", msgList);
		
		request.setAttribute("userId", userId);
		request.setAttribute("userName", userName);
		
		
		return "index";
	}
	
	public static void main(String args[]){
		
		String sql = "SELECT U.RID,U.CONTENTID,I.DEPLOYID,U.APPID,U.USERID,U.USERNAME,U.MOBILE,"
				+ "U.TXTIME2,U.TXCOUNT2,U.YTXCOUNT2,U.CLCOUNT2,U.RETRYCOUNT2,U.TXINTERVAL2,"
				+ "I.TSCONTENT,S.USERLOGINNAME,S.NOTICETYPE,S.ORGANID "
				+ "FROM CF_NOTICEUSER U "
				+ "LEFT JOIN CF_NOTICEINFO I ON U .CONTENTID = I.RID "
				+ "LEFT JOIN SYS_USER S ON S.USERID = U.USERID "
				+ "WHERE U.ZHUOMIAN = '1' AND U.STATE2 = '0' AND U.YTXCOUNT2 < U.TXCOUNT2 "
				+ "AND (U.TXTIME2 IS NULL)"
				+ "') ORDER BY CLCOUNT2,TXTIME2";
		System.out.println(sql);
	}
	
}
